import 'package:flutter/material.dart';
import 'package:notification_permissions/notification_permissions.dart';
import 'package:provider/provider.dart';
import 'package:rate_my_app/rate_my_app.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../i18n/i18n.dart';
import '../models/app.dart';
import '../res/style.dart';
import 'about.screen.dart';
import 'config_server_screen.dart';
import 'language_screen.dart';

class SettingScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SettingScreenState();
  }
}

class SettingScreenState extends State<SettingScreen>
    with TickerProviderStateMixin, WidgetsBindingObserver {
  final bannerHigh = 200.0;
  bool enabledNotification = true;

  RateMyApp _rateMyApp = RateMyApp(
    minDays: 7,
    minLaunches: 10,
    remindDays: 7,
    remindLaunches: 10,
  );

  @override
  void initState() {
    super.initState();

    Future.delayed(Duration.zero, () async {
      checkNotificationPermission();
    });
  }

  void checkNotificationPermission() async {
    try {
      NotificationPermissions.getNotificationPermissionStatus().then((status) {
        if (mounted)
          setState(() {
            enabledNotification = status == PermissionStatus.granted;
          });
      });
    } catch (err) {
//      print(err);
    }
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      checkNotificationPermission();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            backgroundColor: Theme.of(context).backgroundColor,
            expandedHeight: bannerHigh,
            floating: true,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              title: Text(S.of(context).setting,
                  style: TextStyle(
                      fontSize: 18,
                      color: Colors.white,
                      fontWeight: FontWeight.w600)),
            ),
          ),
          SliverList(
            delegate: SliverChildListDelegate(
              <Widget>[
                Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      SizedBox(height: 30.0),
                      Divider(
                        color: Colors.black12,
                        height: 1.0,
                        indent: 75,
                        //endIndent: 20,
                      ),
                      Card(
                        margin: EdgeInsets.only(bottom: 2.0),
                        elevation: 0,
                        child: GestureDetector(
                          onTap: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => Language()));
                          },
                          child: ListTile(
                            leading: Icon(
                              Icons.language,
                              color: Theme.of(context).accentColor,
                              size: 24,
                            ),
                            title: Text(S.of(context).language),
                            trailing: Icon(
                              Icons.arrow_forward_ios,
                              size: 18,
                              color: kGrey600,
                            ),
                          ),
                        ),
                      ),
                      Divider(
                        color: Colors.black12,
                        height: 1.0,
                        indent: 75,
                        //endIndent: 20,
                      ),
                      Card(
                        margin: EdgeInsets.only(bottom: 2.0),
                        elevation: 0,
                        child: SwitchListTile(
                          secondary: Icon(
                            Icons.dashboard,
                            color: Theme.of(context).accentColor,
                            size: 24,
                          ),
                          value: Provider.of<AppModel>(context).darkTheme,
                          activeColor: Color(0xFF0066B4),
                          onChanged: (bool value) {
                            if (value) {
                              Provider.of<AppModel>(context).updateTheme(true);
                            } else
                              Provider.of<AppModel>(context).updateTheme(false);
                          },
                          title: Text(
                            Provider.of<AppModel>(context).isDarkTheme()
                                ? S.of(context).dayMode
                                : S.of(context).nightMode,
                            style: TextStyle(fontSize: 16),
                          ),
                        ),
                      ),
                      Divider(
                        color: Colors.black12,
                        height: 1.0,
                        indent: 75,
                        //endIndent: 20,
                      ),
                      Card(
                        margin: EdgeInsets.only(bottom: 2.0),
                        elevation: 0,
                        child: ListTile(
                          onTap: () {
                            _rateMyApp
                                .showRateDialog(context)
                                .then((v) => setState(() {}));
                          },
                          leading: Image.asset(
                            'assets/images/rate/icon-star.png',
                            width: 24,
                            color: Theme.of(context).accentColor,
                          ),
                          title: Text(S.of(context).rateTheApp,
                              style: TextStyle(fontSize: 16)),
                          trailing: Icon(Icons.arrow_forward_ios,
                              size: 18, color: kGrey600),
                        ),
                      ),
                      Divider(
                        color: Colors.black12,
                        height: 1.0,
                        indent: 75,
                        //endIndent: 20,
                      ),
                      Card(
                        margin: EdgeInsets.only(bottom: 2.0),
                        elevation: 0,
                        child: GestureDetector(
                          onTap: () async {
                            final prefs = await SharedPreferences.getInstance();
                            String server = prefs.getString('server');
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) =>
                                        ConfigServerScreen(server: server)));
                          },
                          child: ListTile(
                            leading: Icon(
                              Icons.computer,
                              color: Theme.of(context).accentColor,
                              size: 24,
                            ),
                            title: Text(S.of(context).server),
                            trailing: Icon(
                              Icons.arrow_forward_ios,
                              size: 18,
                              color: kGrey600,
                            ),
                          ),
                        ),
                      ),
                      Divider(
                        color: Colors.black12,
                        height: 1.0,
                        indent: 75,
                        //endIndent: 20,
                      ),
                      Card(
                        margin: EdgeInsets.only(bottom: 2.0),
                        elevation: 0,
                        child: GestureDetector(
                          onTap: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => AboutScreen()));
                          },
                          child: ListTile(
                            leading: Icon(
                              Icons.trip_origin,
                              color: Theme.of(context).accentColor,
                              size: 24,
                            ),
                            title: Text(S.of(context).about),
                            trailing: Icon(
                              Icons.arrow_forward_ios,
                              size: 18,
                              color: kGrey600,
                            ),
                          ),
                        ),
                      ),
                      Divider(
                        color: Colors.black12,
                        height: 1.0,
                        indent: 75,
                        //endIndent: 20,
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}
